import {
  Column,
  Entity,
  JoinColumn,
  OneToOne,
  ManyToMany,
  JoinTable,
  OneToMany
} from 'typeorm'
import { ApiProperty } from '@nestjs/swagger'

import {
  BaseModelsWithUUIDPrimary,
  AuthEntity,
  PostCommentEntity,
  PostEntity,
  NicknameList
} from './index'

/**
 * @package 用户信息
 * @author liu
 */
@Entity({ name: 'user' })
export class UserEntity extends BaseModelsWithUUIDPrimary {
  @ApiProperty({ example: '昵称', description: '昵称' })
  @Column({ default: null, comment: '昵称' })
  nickname: string

  @ApiProperty({ example: 'example@qq.com', description: '邮箱' })
  @Column({ default: null, comment: '邮箱' })
  email: string

  @ApiProperty({ example: '13579753215', description: '手机号' })
  @Column({ default: null, comment: '手机号' })
  mobile: string

  @ApiProperty({ example: 23, description: '年龄', type: Number })
  @Column({ default: 18, comment: '年龄' })
  age: number

  @ApiProperty({ example: 1, description: '性别', type: Number })
  @Column({ default: 0, comment: '性别' })
  sex: number

  @ApiProperty({
    example: 'https://img01.yzcdn.cn/vant/apple-1.jpg',
    description: '头像'
  })
  @Column({ default: null, comment: '头像' })
  avatar: string

  @ApiProperty({ example: '个人简介', description: '个人简介' })
  @Column({ default: null, comment: '个人简介' })
  description: string

  /** 用户创建商品列表 */
  @OneToMany(type => PostEntity, post => post.user)
  @JoinColumn()
  post: PostEntity[]

  /** 用户收藏商品列表 */
  @ManyToMany(type => PostEntity, post => post.likeUsers)
  @JoinTable({ name: 'user_like_post' })
  likePosts: PostEntity[]

  /** 用户点赞商品列表 */
  @ManyToMany(type => PostEntity, post => post.starUsers)
  @JoinTable({ name: 'user_star_post' })
  starPosts: PostEntity[]

  /** 关注用户列表 */
  @ManyToMany(type => UserEntity, user => user.follows)
  @JoinTable()
  follows: UserEntity[]

  /** 商品评论列表 */
  @OneToMany(type => PostCommentEntity, postComment => postComment.user)
  postComment: PostCommentEntity[]

  @OneToOne(() => AuthEntity, login => login.user)
  @JoinColumn({ name: 'login_id' })
  login: AuthEntity

  constructor() {
    super()
    const i = Math.floor(Math.random() * NicknameList.length)
    this.sex = Math.round(Math.random())
    this.avatar = `https://img01.yzcdn.cn/vant/apple-${
      Math.floor(Math.random() * 8) + 1
    }.jpg`
    this.nickname = NicknameList[i]
  }
}
